package com.zhongli.devplatform.controller.parent;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongli.devplatform.bean.SecurityHelper;
import com.zhongli.devplatform.po.common.ReadClazzOperationPO;
import com.zhongli.devplatform.po.common.ReadCommitOperationPO;
import com.zhongli.devplatform.po.sys.ReadSubjectPO;
import com.zhongli.devplatform.po.teacher.ReadOperationPO;
import com.zhongli.devplatform.po.user.ReadUserPO;
import com.zhongli.devplatform.po.user.ReadUserParentPO;
import com.zhongli.devplatform.po.view.ViewUserPO;
import com.zhongli.devplatform.query.student.StudentQuery;
import com.zhongli.devplatform.service.imp.common.ReadClazzOperationService;
import com.zhongli.devplatform.service.imp.student.ReadCommitOperationService;
import com.zhongli.devplatform.service.imp.student.ReadUserParentService;
import com.zhongli.devplatform.service.imp.sys.ReadSubjectService;
import com.zhongli.devplatform.service.imp.teacher.ReadOperationService;
import com.zhongli.devplatform.service.imp.user.ReadUserService;
import com.zhongli.devplatform.service.imp.view.ViewUserService;
import com.zhongli.devplatform.utils.BeanCopierUtil;
import com.zhongli.devplatform.bean.Res;
import com.zhongli.devplatform.vo.student.StudentVO;
import com.zhongli.devplatform.vo.sys.SubjectVO;
import com.zhongli.devplatform.vo.teacher.TeacherInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author WuXiCheng
 * @version 1.0
 * @date 2020/7/31 9:15
 */
@Api(tags = "家长相关", description = "家长相关", value = "家长相关")
@RestController
@RequestMapping("/parent")
public class ParentController {


    @Autowired
    private ReadUserService userService;

    @Autowired
    private ReadUserParentService parentService;

    @Autowired
    private ReadSubjectService subjectService;

    @Autowired
    private ViewUserService viewUserService;

    @Autowired
    private ReadClazzOperationService clazzOperationService;

    @Autowired
    private ReadOperationService operationService;

    @Autowired
    private ReadCommitOperationService commitOperationService;

    @ApiOperation("家长列表")
    @GetMapping("/learnCondition")
    public Res<List<StudentVO>> learnCondition(@ModelAttribute StudentQuery query){
        return Res.ok(userService.learnCondition(query));
    }

    @ApiOperation("学习报告")
    @PostMapping("/learnReport")
    public Res<List<SubjectVO>> learnReport(){
        List<ReadSubjectPO> list = subjectService.list();
        List<SubjectVO> subjectVOS = BeanCopierUtil.copy(list, SubjectVO.class);
        return Res.ok(subjectVOS);
    }


    @ApiOperation("获取当前家长的个人信息")
    @GetMapping("/getCurrentParentInfo")
    public Res<TeacherInfoVO> getCurrentParentInfo(){
        Integer currentUserId = SecurityHelper.getCurrentUserId();
        ViewUserPO viewUserPO = viewUserService.getOne(new QueryWrapper<ViewUserPO>().eq("user_id",currentUserId));
        TeacherInfoVO infoVO = BeanCopierUtil.copy(viewUserPO, TeacherInfoVO.class);
        infoVO.setId(viewUserPO.getUserId());
        ReadUserParentPO parent = parentService.getOne(new QueryWrapper<ReadUserParentPO>().eq("parent_id", currentUserId));
        if (Objects.isNull(parent)) return Res.ok(infoVO);
        ReadUserPO child = userService.getById(parent.getUserId());
        int operationCount = 0;
        int unfinishedOperation = 0;
        if (child != null) {
            List<ReadClazzOperationPO> allOpera = clazzOperationService
                    .list(new QueryWrapper<ReadClazzOperationPO>().eq("clazz_id", child.getClazzId()));
            if (!allOpera.isEmpty()) {
                List<Integer> operaIds = allOpera.stream().map(ReadClazzOperationPO::getOperationId).collect(Collectors.toList());
                List<ReadOperationPO> list = operationService.listByIds(operaIds);
                Date date = new Date();//在开始时间内的作业题
                List<Integer> dateOperaTds = list.stream()
                        .filter(item -> (date.before(item.getEndTime()) && date.after(item.getStartTime())))
                        .map(ReadOperationPO::getId)
                        .collect(Collectors.toList());
                if (!dateOperaTds.isEmpty()){
                    operationCount = dateOperaTds.size();
                    //operationCount = getOperationCount(operaIds);
                    int fished = commitOperationService.count(new QueryWrapper<ReadCommitOperationPO>()
                            .eq("student_id", parent.getUserId())
                            .in("operation_id", dateOperaTds));
                    unfinishedOperation = operationCount - fished;
                }
            }
        }
        infoVO.setArrangeOperation(operationCount);
        infoVO.setUnfinishedOperation(unfinishedOperation);
        return Res.ok(infoVO);
    }

    /**
     * 获取当月 作业数量
     * @param operaIds 作业ids
     * @return 数量
     */
    private int getOperationCount(List<Integer> operaIds){
        Calendar cale;
        // 获取当月第一天和最后一天
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        // 获取前月的第一天
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        String firstDay = format.format(cale.getTime());
        // 获取前月的最后一天
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        String lastDay = format.format(cale.getTime());

        return operationService.count(new QueryWrapper<ReadOperationPO>()
                .in("id", operaIds)
                .between("start_time",firstDay,lastDay));
    }
}
